package com.earthea.scaffold.common.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

public class PasswordValidatorImpl implements ConstraintValidator<PasswordValidator, String> {
    private static final String SPECIAL_CHARS = "!@#$%^&*()_+-=[]{}|;:',.<>?/";

    @Override
    public boolean isValid(String password, ConstraintValidatorContext context) {
        return isValid(password);
    }

    public static boolean isValid(String password) {
        if (StringUtils.isBlank(password)) {
            return false;
        }
        // 必须包含大写字母、小写字母、数字、特殊字符
        return password.chars().anyMatch(Character::isUpperCase) && password.chars().anyMatch(Character::isLowerCase) && password.chars().anyMatch(Character::isDigit) && password.chars().anyMatch(c -> SPECIAL_CHARS.indexOf(c) != -1);
    }
}
